var express = require('express');
var router = express.Router();
var axios = require('axios')

// 自定义模块
let createTemplate = require('../../utils/sendemail')
const query = require('../../utils/mysql');
const Result = require('../../utils/api');

//获取呢称和头像
router.get('/view/qq/info/:qq', async function (req, res, next) {
    let qq = req.params.qq
    let url = `https://api.usuuu.com/qq/${qq}`
    axios(url).then(body => {
        // console.log(body.data);
        let {
            avatar,
            name: nickname
        } = body.data.data
        Result.success(res, {
            avatar,
            nickname
        })
    }).catch(err => {
        console.log(err, "ERR")
        Result.fail(res, err)
    })
})

// 添加评论
router.post('/view/comment/add', async function (req, res, next) {
    try {
        let params = req.body,
            uid = 0;

        let userSql = 'SELECT * FROM blog_user WHERE email = ? AND qq = ?'
        let userSqlArr = [params.email, params.qq]

        let user = await query(userSql, userSqlArr)
        if (Array.from(user).length === 0) {
            // 评论
            let sql = "INSERT INTO blog_user (qq,nickname,email,avatar,website,add_time) VALUES (?,?,?,?,?,?)"
            let sqlArr = [params.qq, params.nickname, params.email, params.avatar, params.url, new Date()]
            let newUser = await query(sql, sqlArr)
            uid = newUser.insertId
        } else {
            // 回复
            uid = user[0].id
        }
        let sql = `INSERT INTO blog_comment (aid,pid,uid,comment,add_time) VALUES (?,?,?,?,?)`
        let sqlArr = [params.aid, params.cid, uid, params.content, new Date()]
        let commentRes = await query(sql, sqlArr)

        // 邮件通知
        let cid = commentRes.insertId

        let commentSql = "select a.article_title, c.`comment`, c.add_time,u.website,u.nickname, u2.nickname as reply_nickname,u2.email as reply_email from blog_comment c left join blog_article a on c.aid=a.id left join blog_user u on c.uid=u.id left join blog_comment p on c.pid=p.id left join blog_user u2 on p.uid=u2.id WHERE c.id = ?"

        let commentArr = [cid]
        let [commentData] = await query(commentSql, commentArr)

        createTemplate(commentData)

        Result.success(res, 'SUCCESS', '添加留言成功')
    } catch (error) {
        console.log(error)
        Result.fail(res, "添加留言失败")
    }
})

// 评论列表
router.post('/view/comment/list', async function (req, res, next) {
    let aid = req.body.aid

    try {
        let sql = 'select c1.id,c1.pid,c1.`comment`,c1.add_time,u.nickname,u.avatar,u.website,u2.nickname as replyname from blog_comment c1 join blog_user u on c1.uid = u.id left join blog_comment c2 on c1.pid = c2.id left join blog_user u2 on c2.uid = u2.id where c1.aid=? ORDER BY c1.add_time DESC;'
        let sqlArr = [aid]
        let comments = await query(sql, sqlArr)

        let countSql = 'SELECT COUNT(*) as count FROM blog_comment WHERE aid = ?'
        let count = await query(countSql, sqlArr)

        Result.success(res, {
            comments: Array.from(comments),
            count: count[0].count
        })

    } catch (error) {
        Result.fail(res, "参数错误")
    }
})

module.exports = router;